/*
** epur_file_name.c for libkorasm in /home/vanet_c//proj-kw/lib_kor_asm
** 
** Made by charles-edouard vanet
** Login   <vanet_c@epitech.net>
** 
** Started on  Sat Dec  8 17:23:13 2012 charles-edouard vanet
** Last update Thu Dec 13 17:52:01 2012 charles-edouard vanet
*/

#include	<stdlib.h>

char		*my_epur_str(char *buff)
{
  int		idx;
  int		res;

  idx = 0;
  res = 0;
  while (buff[idx] == ' ' || buff[idx] == '\t')
    idx++;
  while (buff[idx] != '\0')
    {
      if (buff[idx] != ' ' && buff[idx] != '\t')
	buff[res++] = buff[idx];
      if (buff[idx + 1] == ' ' || buff[idx + 1] == '\t')
	{
	  while (buff[idx + 1] == ' ' || buff[idx + 1] == '\t')
	    idx++;
	  buff[res++] = ' ';
	}      
      idx = idx + 1;
    }
  buff[res] = '\0';
  if (res > 0 && buff[res - 1] == ' ')
    buff[res - 1] = '\0';
  return (buff);
}

int	get_file_len(char *str)
{
  int	len;

  len = 0;
  while (str[len] != '\0')
    len++;
  return (len);
}

char	*epur_file_name(char *str)
{
  int	i;
  int	j;
  char	*result;

  i = get_file_len(str);
  j = 0;
  while (str[i] != '/' && i > 0)
    i--;
  if ((result = malloc(get_file_len(str) - i + 1)) == NULL)
    return (NULL);
  if (i > 1 && str[i] == '/')
    {
      while (str[++i] != '\0')
	result[j++] = str[i];
      result[j] = '\0';
    }
  else
    {
      free(result);
      return (str);
    }
  free(str);
  return (result);
}

char	*epur_comments(char *str)
{
  int	i;

  i = 0;
  while (str[i] != '\0' && str[i] != '#')
    i++;
  if (str[i] == '#')
    str[i] = '\0';
    str = my_epur_str(str);
  return (str);
}
